<template>
    <basic-container>
        <span class="primary my-2 w-full text-center" style="font-size: 14px; display: inline-block">
            <span class="danger">*</span>
            该界面功能：对探测到的数据库资产进行记录
        </span>
        <el-form ref="form" label-width="160px" :inline="true" size="small">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="选择国家/省份/城市">
                        <country-province-city-cascader v-model="countryProvinceCity"></country-province-city-cascader>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" @click="searchChange">搜 索</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   :page.sync="page"
                   :before-open="beforeOpen"
                   v-model="form"
                   ref="crud"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @refresh-change="refreshChange"
                   @on-load="onLoad">
            <template slot="menuLeft">
                <excel-export v-bind="excelExportProps"></excel-export>
            </template>
        </avue-crud>
    </basic-container>
</template>

<script>
  import {getList} from "@/youan/api/db_asset_search/dbAssetSearch";
  import countryProvinceCityCascader from "@/youan/components/form/country-province-city-cascader";
  import ExcelExport from "../../../components/excel/excel-export";

  export default {
    components: {ExcelExport, countryProvinceCityCascader},
    data() {
      return {
        form: {},
        countryProvinceCity: [],
        query: {},
        loading: true,
        page: {
          pageSize: 50,
          currentPage: 1,
          total: 0
        },
        excelExportProps: {
          exportUrl: "/api/db_asset_search/exportExcel",
          prepareQuery: this.prepareQuery,
          loadingText: "",
          excelName: "数据库资产信息"
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          menu: false,
          addBtn: false,
          dialogClickModal: false,
          header: true,
          column: [
            {
              label: "",
              prop: "id",
              hide: true,
            },
            {
              label: "数据库IP",
              prop: "ip",
            },
            {
              label: "首次出现时间",
              prop: "earliestStartTime",
            },
            {
              label: "IP所在国家",
              prop: "ipCountry",
            },
            {
              label: "IP所在省份",
              prop: "ipProvince",
            },
            {
              label: "IP所在城市",
              prop: "ipCity",
            },
            {
              label: "IP供应商",
              prop: "ipIsp",
            },
            /*{
              label: "",
              prop: "ipUser",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },*/
          ]
        },
        data: []
      };
    },
    methods: {
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchChange() {
        let params = {};
        if(this.countryProvinceCity) {
          params.ipCountry = this.countryProvinceCity[0] || "";
          params.ipProvince = this.countryProvinceCity[1] || "";
          params.ipCity = this.countryProvinceCity[2] || "";
        }
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        });
      },
      prepareQuery() {
        let params = {};
        if(this.countryProvinceCity) {
          params.ipCountry = this.countryProvinceCity[0] || "";
          params.ipProvince = this.countryProvinceCity[1] || "";
          params.ipCity = this.countryProvinceCity[2] || "";
        }
        return params;
      }
    },
    activated() {
      this.$nextTick(()=>{
        if(this.$refs.crud.$refs.table){
          this.$refs.crud.$refs.table.doLayout();
        }
      })
    },
  };
</script>

<style>
</style>
